/*  
 * File name : driver_handle.c
 * Function  : driver handle
 * Date      : 2024-04-16
 * Version   : 0.1.0
 */
/************************************************************************************
***************************************INCLUDE***************************************
*************************************************************************************/
#include "driver_handle.h"
/************************************************************************************
***************************************EXTERN****************************************
*************************************************************************************/

/*
* func        : Handle led Settings
* return      : error code
* input       : <led_direction> (led direction) <cmd> (command)
* output      ：NULL
* author      date     modify
--------------------------------
 xintianyu  2024-4-10  create
*/
int led_handle(DIRECTION led_direction, CMD *cmd)
{
    char *device = "/dev/CEBSS_led";
    static int fd;
    int ret;
	char buf[2];

    /* 打开文件 */
	fd = open(device, O_RDWR);
	if (fd == -1)
	{
		printf("can not open file %s\n", device);
		return -1;
	}

	if (direction_put == led_direction)
	{
		/* write */
		buf[0] = strtol(device, NULL, 0);

		if (cmd_open == *cmd)
			buf[1] = 0;
		if (cmd_close == *cmd)
			buf[1] = 1;
		
		ret = write(fd, buf, 2);
	}
	else if(direction_get == led_direction)
	{
		buf[0] = strtol(device, NULL, 0);
		ret = read(fd, buf, 2);
		if (ret == 2)
		{
			printf("led %d status is %s\n", buf[0], buf[1] == 0 ? "on" : "off");
            if (buf[1] == 0)
            {
                *cmd = cmd_open;
            }
            else if (buf[1] == 1)
            {
                *cmd = cmd_close;
            }
		}
	}
    else
    {
        printf("Undefined command\n");
    }
	
	close(fd);
    return NOERROR;
}

/*
* func        : Handle sr501 Settings
* return      : error code
* input       : NULL
* output      ：<data> (1:someone 0:unmanned)
* author      date     modify
--------------------------------
 xintianyu  2024-4-17  create
*/
int sr501_handle(int* data)
{
    char *device = "/dev/CEBSS_sr501";
    int ret = NOERROR;
	static int fd;
	int val;

    /* 打开文件 */
	fd = open(device, O_RDWR);
	if (fd == -1)
	{
		printf("can not open file %s\n", device);
		return ERROR;
	}

	if (read(fd, &val, 1) > 0)
	{
		if (val  == 1)
		{
			*data = 1;
			printf("get button: %#x ,有人\n", val);
		}
		else
		{
			*data = 0;
			printf("get button: %#x ,无人\n", val);
		}
	}
	else
	{
		printf("get button: -1\n");
	    ret = ERROR;
	}
	close(fd);
    return ret;
}

/*
* func        : Handle sr04 Settings
* return      : error code
* input       : NULL
* output      ：<data> (distance unit:cm)
* author      date     modify
--------------------------------
 xintianyu  2024-4-18  create
*/
int sr04_handle(int *data)
{
    char *device = "/dev/CEBSS_sr04";
    int ret = NOERROR;
	static int fd;
	struct pollfd fds[1];
	int val;
    /* 打开文件 */
	fd = open(device, O_RDWR);
	if (fd == -1)
	{
		printf("can not open file %s\n", device);
		return ERROR;
	}


	ioctl(fd, CMD_TRIG);
	printf("I am goning to read distance: \n");
	
	fds[0].fd = fd;
	fds[0].events = POLLIN;

	if (1 == poll(fds, 1, 5000))
	{
		if (read(fd, &val, 4) == 4)
		{
			printf("get distance: %d cm\n", val*17/1000000);
			*data = val*17/1000000;
	    }
		else
			printf("get distance err\n");
	}
	else
	{
		printf("get distance poll timeout/err\n");
		ret = ERROR;
	}
	if (*data > 450)
	{
		ret = ERROR;
	}
	close(fd);
    return ret;
}

/*
* func        : Handle motor Settings
* return      : error code
* input       : <data> (Number of steps)
* output      ：NULL
* author      date     modify
--------------------------------
 xintianyu  2024-4-18  create
*/
int motor_handle(int data)
{
    char *device = "/dev/CEBSS_motor";
	int buf[2];
    int ret = NOERROR;
	static int fd;
    /* 打开文件 */
	fd = open(device, O_RDWR);
	if (fd == -1)
	{
		printf("can not open file %s\n", device);
		return ERROR;
	}
	printf("步进次数%d\n", data);
    /*5.625° 64：1 4096--360*/
	if (0 == data)
	    data = 365;
	data = data*4096;
	buf[0] = data/360;
	buf[1] = 2;

	if (write(fd, buf, 8) > 0)
	{
		ret = NOERROR;
	}
	else
	{
		ret = ERROR;
	}
	close(fd);
    return ret;
}

/*
* func        : Handle irda Settings
* return      : error code
* input       : NULL
* output      ：<data> (irda cmdcode)
* author      date     modify
--------------------------------
 xintianyu  2024-4-19  create
*/
int irda_handle(char *data)
{
	/*传入参数后面要做通用处理使用空指针*/
    char *device = "/dev/CEBSS_irda";
	unsigned char buf[2];
    int ret = NOERROR;
	static int fd;
    /* 打开文件 */
	fd = open(device, O_RDWR);
	if (fd == -1)
	{
		printf("can not open file %s\n", device);
		return ERROR;
	}
read:
	if (read(fd, buf, 2) == 2)
	{
		printf("get irda: deivce 0x%02x, data 0x%02x\n", buf[0], buf[1]);
		switch(buf[1])
		{
            case 0x00: goto read;
			case 0xa2: *data='r';break;
			case 0xe2: *data='m';break;
			case 0x22: *data='t';break;
			case 0x02: *data='+';break;
			case 0xc2: *data='b';break;
			case 0xe0: *data='l';break;
			case 0xa8: *data='p';break;
			case 0x90: *data='r';break;
			case 0x68: *data='0';break;
			case 0x98: *data='-';break;
			case 0x30: *data='1';break;
			case 0x18: *data='2';break;
			case 0x7a: *data='3';break;
			// case 0x7a: *data='4';break;
			// case 0x7a: *data='5';break;
			// case 0x7a: *data='6';break;
			case 0x42: *data='7';break;
			// case 0x7a: *data='8';break;
			case 0x52: *data='9';break;
			// case 0x52: *data='c';break;
			default:*data='e';printf("no this key!!!\n");break;
		}
		printf("data == %c\n", *data);
	}
	else
	{
		printf("get irda: -1\n");
	    ret = ERROR;
	}

	close(fd);
    return ret;
}

/*
* func        : Handle dht11 Settings
* return      : error code
* input       : NULL
* output      ：<data> (tcp frames)
* author      date     modify
--------------------------------
 xintianyu  2024-4-20  create
*/
int dht11_handle(char *data)
{
	/*传入参数后面要做通用处理使用空指针*/
    char *device = "/dev/cebss_dht11";
	unsigned char buf[2];
    int ret = NOERROR;
	static int fd;
	int flag = 0;
 read:
	if (flag > 5)
	{
		sprintf(data,"@005e");
		ret = ERROR;
		goto err;
	}
	flag++;
    /* 打开文件 */
	fd = open(device, O_RDWR);
	if (fd == -1)
	{
		printf("can not open file %s\n", device);
		return ERROR;
	}
	if (read(fd, buf, 2) == 2)
	{
		printf("get Humidity: %d, Temperature : %d\n", buf[0], buf[1]);
		sprintf(data,"@005g%d%d", buf[0], buf[1]);
		close(fd);
	}
	else
	{
		sleep(3);
		printf("get dht11: -1\n");
		close(fd);
		goto read;
	}
err:
    return ret;
}

/*
* func        : Handle ds18b20 Settings
* return      : error code
* input       : NULL
* output      ：<data> (tcp frames)
* author      date     modify
--------------------------------
 xintianyu  2024-4-22  create
*/
int ds18b20_handle(char *data)
{
	/*传入参数后面要做通用处理使用空指针*/
    char *device = "/dev/cebss_ds18b20";
	int buf[2];
    int ret = NOERROR;
	static int fd;
    /* 打开文件 */
	fd = open(device, O_RDWR);
	if (fd == -1)
	{
		printf("can not open file %s\n", device);
		return ERROR;
	}
	if (read(fd, buf, 8) == 8)
	{
		printf("get ds18b20: %d.%d\n", buf[0], buf[1]);
		sprintf(data,"@006g%d%d", buf[0], buf[1]);
	}
	else
	{
		printf("get ds18b20: -1\n");
	    sprintf(data,"@006e");
	}
	return ret;
}

/*
* func        : Handle at24c02 Settings
* return      : error code
* input       : <direction> (direction_put || direction_get) <len> (data size)
* output      ：<data> (rom value)
* author      date     modify
--------------------------------
 xintianyu  2024-4-22  create
*/
int at24c02_handle(DIRECTION direction, char *data, int len)
{
	/*传入参数后面要做通用处理使用空指针*/
	/*路径宏定义*/
    char *device = "/dev/cebss_at24c02";
    int ret = NOERROR;
	static int fd;
	char buf[100];
    /* 打开文件 */
	fd = open(device, O_RDWR);
	if (fd == -1)
	{
		printf("can not open file %s\n", device);
		return ERROR;
	}
	if (direction_put == direction)
	{
		strncpy(buf, data, len);
		ret = write(fd, data, len+1);
		printf("ret == %d\n", ret);
		if (ret >= 0)
	    {
			buf[len] = '\0';
			printf("write %s SUCCESS\n", buf);
            sprintf(data,"@007p");
			ret = NOERROR;
		}
		else
		{
			sprintf(data,"@007e");
			printf("at24c02: PUT ERROR!!!\n");
			ret = ERROR;
		}
	}
	else if (direction_get == direction)
	{
		ret = read(fd, buf, 100);
		if (ret > 0)
	    {
            sprintf(data,"@007g%s", buf);
		    printf("data: %s\n", data);
			ret = NOERROR;
		}
		else
		{
			sprintf(data,"@007e");
			printf("at24c02: GET ERROR!!!\n");
			ret = ERROR;
		}
	}
	else
	{
        sprintf(data,"@007e");
		printf("at24c02: CMD ERROR!!!\n");
	}
	return ret;
}

/*
* func        : Handle dac Settings
* return      : error code
* input       : <data> (value)
* output      ：NULL
* author      date     modify
--------------------------------
 xintianyu  2024-4-24  create
*/
int dac_handle(int *data)
{
	/*传入参数后面要做通用处理使用空指针*/
	/*路径宏定义*/
    char *device = "/dev/cebss_dac";
    int ret = NOERROR;
	int dac_val = 0;
	static int fd;

    /* 打开文件 */
	fd = open(device, O_RDWR);
	if (fd == -1)
	{
		printf("can not open file %s\n", device);
		return ERROR;
	}

    /*0~1023*/
    dac_val = *data*1023/100+1;
	printf("dac_val == %d\n", dac_val);
	write(fd, &dac_val, 2);
	dac_val += 50;

	close(fd);
	return ret;
}